(function(){
	$.fn.chart = function(ops){
		var p = navigator.platform;
		if(p && p.indexOf("Linux")==0){
			this.text($i18n("ccay.common.chart.android"));
			return;
		}
		var op = $.extend(true,{
			type:'line',
			width:500,
			height:300,
			lines:null,
			linesOp:null,
			ajax:null,
			settings:{
				grid:{},
				legend:{},
//				xaxis:{},
//				yaxis:{},
//				xaxes:[],
//				yaxes:[],
				series:{
					pie:{
						label:{
							show:true,
							radius: 2/5,
							formatter: chartHelper.formatPieLabel
						}
					}
				}
			},
			fnClick:null
		},ops);
		
		this.width(op.width).height(op.height);

		chartHelper.setType(op);
		if($.isPlainObject(op.lines)){
			op.lineSet = op.lines;
			op.lines = [];				
		}
				
		if(op.ajax)
		{
			var chart = this;
			if(op.ajax.filter)
			{
				Ccay.UI.ajax.setFnSuccess(op.ajax,function(datas){
					if($.isArray(datas))
					{
						if(op.lines){
							for(var i=0,len = op.lines.length;i < len;i++)
							{
								op.lines[i].data = datas[i];
							}
						}
						else
							op.lines = datas;
						
						chartHelper.buildChart(chart,op);
					}
				});
			}
			else
				Ccay.UI.ajax.setFnSuccess(op.ajax,function(datas){
					if(datas.result)
						datas = datas.result;
					
					if($.isArray(datas))
					{
						if(op.lineSet)
						{
							for(var i=0,len = datas.length;i < len;i++){
								chartHelper.buildLine(op,datas[i]);
							}
						}
						else{
							for(var i=0,len = op.lines.length;i < len;i++)
							{
								chartHelper.formatData(op.lines[i],datas);
							}
						}
						
						chartHelper.buildChart(chart,op);
					}
				});
			
			Ccay.UI.ajax(op.ajax,this);
		}
		else
			chartHelper.buildChart(this,op);

	}
	
	var chartHelper = {
			setType:function(op){
				switch(op.type){
					case "bar":
						this.showSeries(op,'bars');
						break;
					case "point":
						this.showSeries(op,'points');
						break;
					case "pie":
						op.settings.series.pie.show = true;
						$.extend(true,op.settings.series.pie,op.linesOp);
						if(op.settings.series.pie.label.show && op.settings.legend.show == void 0)
							op.settings.legend.show = false;
						break;
					case "line-point":
						this.showSeries(op,'lines');
						this.showSeries(op,'points');
						break;
				}
			},
			showSeries:function(op,type){
				if(op.linesOp)
					op.linesOp.show = true;
				else
					op.linesOp = {show:true};
					
				op.settings.series[type] = op.linesOp;
			},
			buildLine:function(op,data){
				var line = $.extend(true,{},op.lineSet);
				line.label = Ccay.UI.template(line.label,data);
				if(line.fnSetData)
					line.data= [[line.fnSetData(data)]];
				else
					line.data= [[data[line.xField],data[line.yField]]];
				op.lines.push(line);
			},
			formatData:function(line,datas){
				if(line.totalField){
					var total = 0;
					$.each(datas,function(i,item){
						if(line.fnSetData)
							total += line.fnSetData(item);
						else
							total += item[line.totalField];
					});
					line.data = total;
				}
				else if((line.xField && line.yField) || line.fnSetData){
					var lineData = [];
					$.each(datas,function(i,item){
						if(line.fnSetData)
							lineData.push(line.fnSetData(item));
						else
							lineData.push([item[line.xField],item[line.yField]]);
					});
					line.data  = lineData;
				}
			},
			buildChart:function(chart,op){
				this.bindClick(chart,op);
					//this.bindHover(chart,op);
				$.plot(chart,op.lines, op.settings);
			},
			bindClick:function(chart,op){
				if(op.fnClick){
					op.settings.grid.clickable = true;
					var fnClick = op.fnClick;
					chart.bind("plotclick",function(e,pos,item){
						fnClick(item ? item.datapoint : [pos.x.toFixed(2),pos.y.toFixed(2)]);
					});
				}
			},
			bindHover:function(chart,op){
				if(op.settings.fnHover || op.settings.showTip){
					if(!op.settings.grid)
						op.settings.grid = {};
					
					op.settings.grid.hoverable = true
					
					var fnHover = op.fnHover,showTip = op.showTip;
					
					chart.bind("plothover",function(e, pos, item){
						if(fnHover)
							fnHover(item);
					     
						if(showTip){
							chartHelper.showTip(item);
						}
					});
					//$.create('div').id('chart-tip').insertAfter(chart);
				}
			},
			showTip:function(item){
//				var tip = $('#chart-tip');
//				if(item){
//					
//				}
			},
			formatPieLabel:function(label, slice){
				var title,tmpl;
				if(label.indexOf('${') < 0)
				{
					title = label;
					tmpl = '<span class="title">${title}</span><span class="data">${data}</span><span class="percent">${percent}%</span>'
				}
				else
				{
					title ='';
					tmpl = label;
				}
				var data = {title:title,percent:Math.round(slice.percent),data:slice.data[0][1]};
				return'<div class="ccay-chart-label">' + Ccay.UI.template(tmpl,data) + '</div>';
			}
	};
}());